<template>
  <div class="mian">
    <el-card style="height: 100%">
      <div class="top">
        <span
          v-for="(item, index) in NameType"
          :key="index"
          :class="['top-son', { lower: topIndex === index }]"
          :style="topIndex === index ? 'color:#16171C' : 'color:#e7e7e7'"
          @click="TopMenu(item, index)"
          >{{ item.arrangetype }}</span
        >
        <el-icon><CirclePlus color="#547bf1" @click="show" /></el-icon>
      </div>
      <el-button @click="btnAdd" class="btn-add" type="primary"
        >新增人员</el-button
      >
      <el-button @click="edit(true)" class="btn-add" type="primary"
        >编辑</el-button
      >

      <!-- <span class="btn-add" @click="btnAdd">新增人员</span>  -->
      <!-- <span class="btn-add" style="width: 74px" @click="edit(true)">编辑</span> -->
      <div class="Scheduling-title">
        {{ monment().format('YYYY年MM月') }}
        <div class="Scheduling-btn" v-if="btnIs">
          <el-button
            type="primary"
            style="background-color: #1e6ae7"
            @click="preservation(0)"
            >保存</el-button
          >
          <el-button type="info" plain @click="preservation(1)">取消</el-button>
        </div>
      </div>
      <div class="Schedulingbody">
        <div class="Scheduling-main">
          <div class="time-day">
            <div class="Scheduling-top time-day">
              <text>姓名</text>
            </div>
            <div
              class="Scheduling-top"
              v-for="(item, index) in tiem"
              :key="index"
            >
              {{ item }}
            </div>
          </div>
        </div>
        <div class="Scheduling-body">
          <div class="time-day">
            <el-row>
              <el-col :span="24">
                <div v-for="(val1, ix) in list.list" :key="ix" class="zhuti">
                  <div class="Scheduling-top time-day block-color">
                    <text>{{ val1.personName }}</text>
                    <div v-if="isDisabled === false">
                      <div v-if="fullter(val1, ix) === 0">
                        <el-icon style="display: block"
                          ><CaretTop color="#E5E5E5"
                        /></el-icon>
                        <el-icon style="display: block"
                          ><CaretBottom
                            color="#999999"
                            @click="down(val1, ix, list)"
                        /></el-icon>
                      </div>
                      <div v-if="fullter(val1, ix) === 1">
                        <el-icon style="display: block"
                          ><CaretTop
                            color="#999999"
                            @click="up(val1, ix, list)"
                        /></el-icon>
                        <el-icon style="display: block"
                          ><CaretBottom
                            color="#999999"
                            @click="down(val1, ix, list)"
                        /></el-icon>
                      </div>
                      <div v-if="fullter(val1, ix) === 2">
                        <el-icon style="display: block"
                          ><CaretTop
                            color="#999999"
                            @click="up(val1, ix, list)"
                        /></el-icon>
                        <el-icon style="display: block"
                          ><CaretBottom color="#E5E5E5"
                        /></el-icon>
                      </div>
                    </div>
                  </div>
                  <div
                    class="Scheduling-top block-color"
                    v-for="(res, index) in val1.arrangeList"
                    :key="index"
                  >
                    <el-select
                      v-model="res.type"
                      class="m-2"
                      placeholder=""
                      size="large"
                      :disabled="isDisabled"
                    >
                      <el-option
                        v-for="item in options"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                      />
                    </el-select>
                    <el-checkbox
                      v-model="res.leader"
                      :disabled="isDisabled"
                      @click="changeRadio($event, res, val1, index, ix)"
                      :label="true"
                      :max="1"
                      size="large"
                      >班长</el-checkbox
                    >
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>
        </div>
      </div>
    </el-card>
    <add-dialog
      :title="dialogTitle"
      :rowData="curRowData"
      :rowName="name"
      :rowTime="dayAll"
      :visible="visibleDialog"
      @onReload="reloadPage"
      @onCloes="onCloes"
    ></add-dialog>
    <add-type
      :title="dialogTitle"
      :rowData="curRowData"
      :visible="TypeDialog"
      @onReload="reloadPage"
      @onCloes="onCloes"
    >
    </add-type>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import {
  ComDutyTypeDrop,
  selectArrangeRecord,
  saveArrangeRecord
} from '@/api/dutyManagement'
import { CaretTop, CaretBottom, CirclePlus } from '@element-plus/icons-vue'
import AddDialog from './components/AddDialog/index.vue'
import AddType from './components/AddDialog/AddType.vue'
import { title } from 'process'
import { isTemplateLiteral } from '@babel/types'
// 值班类型菜单
const NameType = ref([])
const monment = require('moment')
const name = ref()
const topIndex = ref(0)
const date = ref(new Date())
const dayAll = ref([])
const month = ref(date.value.getMonth())
const btnIs = ref(false)
const disble = ref(false)
const tiem = ref()
const isDisabled = ref(true)
const dialogTitle = ref('')
const curRowData = ref({})
const visibleDialog = ref(false)
const TypeDialog = ref(false)
const options = ref([
  {
    value: '01',
    label: '休息'
  },
  {
    value: '02',
    label: '白班'
  },
  {
    value: '03',
    label: '夜班'
  },
  {
    value: '04',
    label: '白+夜'
  }
])
const reloadPage = (data) => {
  console.log('data', data)

  visibleDialog.value = false
  TypeDialog.value = false
  Drop(data)
}
const onCloes = () => {
  visibleDialog.value = false
  TypeDialog.value = false
  curRowData.value = {}
}
const show = () => {
  dialogTitle.value = '新增值班类型'
  TypeDialog.value = true
}
// 获取当前月份的日期
// 获取当前月份的日期
const getEveryDay = (date, month) => {
  date.setMonth(month + 1)
  date.setDate(0)
  let dayArry = []
  //获取月份的某一天
  let day = date.getDate()
  for (let i = 1; i <= day; i++) {
    const currentDate = new Date(
      date.getTime() - (day - i) * 24 * 60 * 60 * 1000
    )
    dayArry.push(i + '日' + getWeekday(currentDate.getDay()))
  }
  for (let i = 1; i <= day; i++) {
    dayAll.value.push(monment().format('YYYY-MM-') + (i < 10 ? '0' + i : i))
  }
  tiem.value = dayArry
}
const getWeekday = (day) => {
  return ['周日', '周一', '周二', '周三', '周四', '周五', '周六'][day]
}
getEveryDay(date.value, month.value)
// const getEveryDay = (date, month) => {
//   date.setMonth(month + 1)
//   //设置一个月的某一天-这里设置为零则取到的日期中的天就会是当月的最后一天（比如，二月份就是28或29，其他月份就是30或31），方便下边的循环
//   date.setDate(0)
//   let dayArry = []
//   //获取月份的某一天
//   let day = date.getDate()
//   for (let i = 1; i <= day; i++) {
//	  dayArry.push(monment().format('YYYY-MM-') + (i < 10 ? '0' + i : i))
//     dayArry.push(i < 10 ? '0' + i : i)
//   }
//   tiem.value = dayArry
// }
// getEveryDay(date.value, month.value)

// 值班主体
const list = ref({})

const Drop = async (data) => {
  const res = await ComDutyTypeDrop()
  NameType.value = res
  if (res.length > 0) {
    name.value = res[0].arrangetype
    Record(res[0].arrangetype)
  }
  if (data) {
    NameType.value.push({ arrangetype: data })
  }
  console.log('NameType', NameType.value)
}
Drop()

const Record = async (itme) => {
  console.log('item', itme)
  const item = await selectArrangeRecord(itme)
  console.log('item', item)
  list.value.list = item.list
  // console.log('isTemplateLiteral', isTemplateLiteral)
  // list.value = isTemplateLiteral
}
// 顶部菜单点击事件
const TopMenu = (item, index) => {
  console.log('changeaaa')
  console.log('changeaaa', item)
  console.log('changeaaa', index)
  if (btnIs.value) return
  Record(item.arrangetype)
  name.value = item.arrangetype
  topIndex.value = index
}
//新增人员
const btnAdd = () => {
  dialogTitle.value = '新增人员'
  visibleDialog.value = true
  curRowData.value = NameType.value
}
//编辑
const edit = (item) => {
  isDisabled.value = !isDisabled.value
  btnIs.value = !btnIs.value
}
// 保存 or 取消
const preservation = async (item) => {
  if (item === 0) {
    let parmas = {
      arrangeType: name.value,
      list: list.value.list
    }
    let res = await saveArrangeRecord(parmas)
    isDisabled.value = !isDisabled.value
    btnIs.value = !btnIs.value
    Drop()
  } else {
    isDisabled.value = !isDisabled.value
    btnIs.value = !btnIs.value
    Drop()
  }
}
/**
 * 通过选中的莫某个日期下表来将其他的人物同一个日期修改为false
 * res选中的复选框按钮
 * val1选中的人物姓名一行所有排班
 * index一行的第几个
 * ix行的下表
 */
const changeRadio = (e, res, val1, index, ix) => {
  console.log('e', e)
  console.log('res', res)
  if (e.target.tagName === 'INPUT') return
  list.value.list.map((item, id) => {
    item.arrangeList[index].leader = false
    if (id === ix) {
      console.log('item', item)
    }
  })
}
const fullter = (data, index) => {
  let last = index + 1
  if (index === 0) {
    return 0
  } else if (index !== 0 && last !== list.value.list.length) {
    return 1
  } else {
    return 2
  }
}
const down = (item, index, list) => {
  console.log('11111', list)
  console.log('index', index)
  // arrIndexExchange(index, index - 1)
  downGo(list.list, index)
}

const up = (item, index, list) => {
  console.log('22222')
  upGo(list.list, index)
}
const upGo = (fieldData, index) => {
  if (index !== 0) {
    fieldData[index] = fieldData.splice(index - 1, 1, fieldData[index])[0]
  } else {
    fieldData.push(fieldData.shift())
  }
}
const downGo = (fieldData, index) => {
  if (index !== fieldData.length - 1) {
    fieldData[index] = fieldData.splice(index + 1, 1, fieldData[index])[0]
  } else {
    fieldData.unshift(fieldData.splice(index, 1)[0])
  }
}
const arrIndexExchange = (x, y) => {
  let array = list.value.list
  console.log('x', x)
  console.log('y', y)
  array.splice(x - 1, 1, ...array.splice(y - 1, 1, array[x - 1]))
  console.log('list.value.list', list.value.list)

  return (list.value.list = Object.assign(array, list.value.list))
}
</script>

<style lang="scss" scoped>
.mian {
  position: relative;
  width: 100%;
  height: 100%;
  .top {
    width: 100%;
    height: 51px;
    line-height: 47px;
    padding-left: 24px;
    padding-right: 24px;
    border-bottom: 1px solid #e7e7e7;
    color: #b7b7b7;
    font-size: 16px;
    .top-son {
      width: 70px;
      height: 47px;
      margin-right: 20px;
      display: inline-table;
      white-space: nowrap;
      text-align: center;
      cursor: pointer;
    }
  }
  .btn-add {
    margin-left: 24px;
    margin-top: 20px;
    font-family: Microsoft YaHei-Regular, Microsoft YaHei;
    text-align: center;
    cursor: pointer;
  }
  .Schedulingbody {
    position: relative;
    height: 78%;
    white-space: nowrap;
    overflow-x: auto;
    margin-right: 24px;
    margin-left: 24px;
    .Scheduling-main {
      position: relative;
      top: 0px;
      left: 0;
      display: flex;
      width: 100%;
      height: 60px;
      font-size: 16px;
    }
    .Scheduling-body {
      position: relative;
      top: 0px;
      left: 0;
      display: flex;
      width: 100%;
      height: 60px;
      font-size: 16px;
    }
    .block-color {
      background: #f8faff !important;
    }
    .Scheduling-top {
      background: #e4edff;
      border-radius: 2px 2px 2px 2px;
      width: 150px;
      height: 60px;
      line-height: 60px;
      margin-right: 2px;
      display: flex;
      align-items: center;
      justify-content: center;
      .m-2 {
        margin-left: 0;
      }
      .ml-4 {
        display: flex;
        align-items: center;
        justify-content: center;
        margin-left: 5px;
      }
    }
    .zhuti {
      display: flex;
      width: 100%;
      height: 60px;
      line-height: 60px;
      text-align: center;
      margin-top: 2px;
    }
    .time-day {
      text-align: center;
      height: 60px;
      line-height: 60px;
      display: flex;
    }
  }
}
.Scheduling-title {
  width: 100%;
  height: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 16px;
  font-weight: bold;
  color: #333333;
}
.Scheduling-btn {
  position: absolute;
  top: 17%;
  right: 35px;
  z-index: 1;
}
.lower {
  border-bottom: 3px solid #1d6ae6 !important;
}
:deep(.el-card__body) {
  padding: 0;
}
:deep(.el-input__inner) {
  width: 90px !important;
  padding-left: 3px !important;
}
:deep(.el-checkbox__label) {
  padding-left: 3px;
}
:deep(.el-checkbox) {
  padding-left: 3px;
}
</style>
